package com.maoye.ossp.sys.base.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.google.common.collect.Maps;
import com.hotent.base.api.query.QueryFilter;
import com.hotent.base.api.query.QueryOP;
import com.maoye.ossp.common.entity.Page;
import com.maoye.ossp.common.web.BaseController;
import com.maoye.ossp.sys.base.entity.SysBaseFloor;
import com.maoye.ossp.sys.base.entity.SysBaseFloorAttment;
import com.maoye.ossp.sys.base.entity.SysBaseShop;
import com.maoye.ossp.sys.base.service.SysBaseFloorAttmentService;
import com.maoye.ossp.sys.base.service.SysBaseFloorService;
import com.maoye.ossp.sys.base.service.SysBaseRegionService;
import com.maoye.ossp.sys.base.service.SysBaseShopService;
import com.maoye.ossp.sys.org.util.UserUtils;
import com.maoye.sc.api.domain.User;
import com.maoye.sc.api.local.UserService;
@Controller
@RequestMapping(value = "/sys/base/floorAtt/")
public class SysBaseFloorAttController extends BaseController {

	@Resource
	SysBaseFloorAttmentService sysBaseFloorAttmentService;
	
	@Resource 
	UserService<User> userService;
	@Resource
	SysBaseFloorService sysBaseFloorService;
	@Resource
	SysBaseRegionService sysBaseRegionService;
	@Resource
	SysBaseShopService sysBaseShopService;
	
	/**
	 * 获得楼层列表
	 * @param request
	 * @param responser
	 * @throws IOException
	 */
	@RequestMapping(value = "list")
	public String list(SysBaseFloorAttment floor,HttpServletRequest request, HttpServletResponse response,Model model,SysBaseFloorAttment floorAtt) {
		QueryFilter queryFilter = getQuerFilter(request,floor);
		Page<SysBaseFloorAttment> page = (Page<SysBaseFloorAttment>)sysBaseFloorAttmentService.find(queryFilter);

		model.addAttribute("SysBaseFloorAttment",floor);
		model.addAttribute("page", page);
		return resultPage("/sys/base/floor/list", model, request);
	}
	
	
	@RequestMapping(value ="addAttFloor")
	public String addAttFloor(HttpServletRequest request,HttpServletResponse response,Model model){	
		SysBaseFloorAttment sysBaseFloor=new SysBaseFloorAttment();
		sysBaseFloor.setId(idGenerator.getUId());
		sysBaseFloor.setCreateTime(new Date());
		sysBaseFloor.setCreateBy(UserUtils.getCurrentUserId().toString());
		User createByUser =userService.getUserById(UserUtils.getCurrentUserId().toString());
		model.addAttribute("createByName", createByUser.getName());
		model.addAttribute("sysBaseFloorAttment",sysBaseFloor);
		return "/sys/base/floor/addAtt"; 
	}
	
	@RequestMapping(value ="saveAttFloor")
	public String saveAttFloor(SysBaseFloorAttment floor,HttpServletRequest request,HttpServletResponse response,RedirectAttributes redirectAttributes) throws IOException{
		
		HashMap<String, Object> map =Maps.newHashMap();
		
		SysBaseFloor baseFloor=sysBaseFloorService.get(floor.getFloorid());
		floor.setFloorNo(baseFloor.getCode());
		floor.setShopNo(baseFloor.getParent());
		map.put("no", baseFloor.getParent());
		SysBaseShop shop=sysBaseShopService.getByNo(map);
		floor.setAreaNo(shop.getRegionCode());
//		
		if(sysBaseFloorAttmentService.get(floor.getId())!=null){
			sysBaseFloorAttmentService.update(floor);
		}else{
			sysBaseFloorAttmentService.create(floor);
		}
		addMessage(redirectAttributes, "保存成功");
		return "redirect:/sys/base/floorAtt/list";
	}
	
	
	/**
	 * 查看门店信息
	 * @param region
	 * @param model
	 * @param request
	 * @return
	 * @throws IOException 
	 * @throws ServletException 
	 */
	@RequestMapping(value ="viewFloor")
	public String viewFloor(HttpServletRequest request, HttpServletResponse response,Model model){
		String id =request.getParameter("id");
		
		SysBaseFloorAttment  flooratt=sysBaseFloorAttmentService.get(Long.parseLong(id));
			//添加门店名称
		model.addAttribute("floor",flooratt);
		return "/sys/base/floor/viewAtt";
	}
	
	/**
	 * 查看门店信息
	 * @param region
	 * @param model
	 * @param request
	 * @return
	 * @throws IOException 
	 * @throws ServletException 
	 */
	@RequestMapping(value ="ckeckAttFloor")
	public void ckeckAttFloor(HttpServletRequest request, HttpServletResponse response,Model model) throws IOException{
		response.setCharacterEncoding("utf-8");
		String id =request.getParameter("floorid");
		SysBaseFloor baseFloor=sysBaseFloorService.get(id);
		SysBaseFloorAttment floor = new SysBaseFloorAttment();
		QueryFilter queryFilter = getQuerFilter(request,floor);
		queryFilter.addFilter("floorNo", baseFloor.getCode(), QueryOP.EQUAL);
		queryFilter.addFilter("shopNo", baseFloor.getParent(), QueryOP.EQUAL);
		Page<SysBaseFloorAttment> page = (Page<SysBaseFloorAttment>)sysBaseFloorAttmentService.find(queryFilter);
		List<SysBaseFloorAttment> attList=page.getList();
		PrintWriter out = response.getWriter();
		StringBuilder jsonStr = new StringBuilder();
		if(attList!=null&&!attList.isEmpty()){
			jsonStr.append("false");
		}else{
			jsonStr.append("true");
		}

		out.print(jsonStr.toString());
		out.flush();
		out.close();
	}
	
	
	@RequestMapping(value ="updateFloor")
	public String updateFloor(HttpServletRequest request,HttpServletResponse response,Model model){
		String id =request.getParameter("id");

		SysBaseFloorAttment  flooratt=sysBaseFloorAttmentService.get(Long.parseLong(id));
		User createByUser =userService.getUserById(UserUtils.getCurrentUserId().toString());
		flooratt.setUpdateTime(new Date());
		model.addAttribute("createByName", createByUser.getName());
		
		User updateByUser =userService.getUserById(UserUtils.getCurrentUserId().toString());
		model.addAttribute("updateByName", updateByUser.getName());
		model.addAttribute("sysBaseFloorAttment",flooratt);
		return "/sys/base/floor/addAtt";
	}
	
	
	@RequestMapping(value ="deleteFloor")
	public String deleteFloor(HttpServletRequest request,HttpServletResponse response,RedirectAttributes redirectAttributes){
		String id =request.getParameter("id");
		sysBaseFloorAttmentService.remove(Long.parseLong(id));
		addMessage(redirectAttributes, "删除成功");
		return "redirect:/sys/base/floorAtt/list";
	}
}
